package com.wollo.student.controller;

import com.wollo.domain.AjaxResult;
import com.wollo.domain.Check;
import com.wollo.domain.PageResult;
import com.wollo.domain.User;

import com.wollo.student.mapper.CheckMapper;
import com.wollo.student.service.CheckService;
import com.wollo.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * @author OuYang
 * @date 2024/9/1710:37
 * @description
 */
@Controller
@RequestMapping("/sign")
public class SignController {
    @Autowired
    private CheckService checkService;
    @Autowired
    private CheckMapper checkMapper;
    @RequestMapping("/page")
    public String signPage() {
        return "sign";
    }
    //用户签到
    @PostMapping("/userSign")
    @ResponseBody
    public AjaxResult userSign(String signStatus, HttpSession session) throws ParseException {
        //1. 获取当前用户信息
        User user = (User) session.getAttribute("user");
        Integer uid = user.getUid();
        //2. 调用业务层完成签到
        int i = checkService.insCheckInfoService(uid, signStatus);
        //3. 响应结果
        if(i==CheckService.CHECK_CODE_SUCCESS){
            return AjaxResult.ok(300);
        }
        return AjaxResult.ok();
    }
    //分页加载当前用户的签到信息
    @GetMapping("/signUserInfo")
    @ResponseBody
    public PageResult<Check> signUserInfo(Integer page, Integer limit, HttpSession session, String checkDate){
        //获取用户的Id
        User user = (User) session.getAttribute("user");
        return checkService.selectUserCheckInfoService(page,limit,user.getUid(),checkDate);
    }
    //导出用户的考勤信息
    @RequestMapping ("/export")
    public void export(String checkDate, HttpSession session, HttpServletResponse response) throws IOException {
        //查询用户的考勤信息
        User user = (User) session.getAttribute("user");
        //查询所有考勤信息，不带分页
        List<Check> checks = checkMapper.selectUserCheckInfoMapperList(user.getUid(), checkDate);
        //导出考勤信息
        ExcelUtils.exportExcel(response,"考勤信息",checks);
    }
}
